package com.haois.mp.wechat.server;

import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.module.common.daomain.RemindDob;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.offaccounts.service.RemindDobService;
import com.haois.mp.database.module.common.service.IUserTokenService;
import com.haois.mp.database.util.jx.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/8/4 10:42
 */
@Component("BirRemindServer")
public class BirRemindServerImpl extends AbstractMoreInfoServer {

    @Autowired
    private RemindDobService remindDobService;
    @Autowired
    private IUserTokenService iUserTokenService;

    @Override
    public String execute(WxMpXmlMessage wxMpXmlMessage) {
        List<RemindDob> nearBirthday = new ArrayList<>();
        Date date = new Date();
        StringBuffer content = new StringBuffer("");
        content.append("以下成员30天内过生日:");
        UserToken userToken = iUserTokenService.findByOpenId(wxMpXmlMessage.getFromUser(), MpTypeEnum.OFFACCOUNTS.getType());
        List<RemindDob> remindDobs = remindDobService.findByMember(userToken.getUserId(), null, null);
        remindDobs.forEach(remindDob -> {
            int daysBetween = DateUtil.daysBetween(date, remindDob.getOnBirthday());
            if (daysBetween < 30 && daysBetween > 0) {
                nearBirthday.add(remindDob);
            }
        });
        if (nearBirthday.isEmpty()) {
            content.append("\n").append("近期无成员生日");
        } else {
            nearBirthday.forEach(remindDob -> {
                content.append("\n")
                        .append("☞☞ ").append(remindDob.getName()).append("：").append(remindDob.getAge()).append("#")
                        .append(DateUtil.getTime(DateUtil.Md, remindDob.getOnBirthday()));
            });
        }

        return content.toString();
    }

    @Override
    public String executePlus(WxMpXmlMessage wxMpXmlMessage, Object object) {
        UserToken userToken = (UserToken) object;
        String texts[] = wxMpXmlMessage.getContent().split(" ");
        if (texts.length < 4) {
            return "模板格式错误，应为【生日提醒 [姓名] [新/旧] [yyyy-MM-dd] [提醒内容]】";
        }
        if (texts[1].length() > 50) {
            return "名字不能大于50";
        }
        if (!(texts[2].equals(RemindDob.SOLAR) || texts[2].equals(RemindDob.LUNAR))) {
            return "生日类型有误，应为【[新/旧]】";
        }
        Date date = DateUtil.stringToDate(texts[3], DateUtil.yMd);
        if (date == null) {
            return "时间格式有误，应为【[yyyy-MM-dd]】";
        }
        for (int i = 4; i <= texts.length - 1; i++) {
            texts[4] += " " + texts[i];
        }
        RemindDob remindDob = new RemindDob();
        remindDob.setBirthday(date);
        remindDob.setBirthdayType(texts[2]);
        remindDob.setName(texts[1]);
        remindDob.setMemberNum(userToken.getUserId());
        remindDob.setContent(texts[4]);
        remindDobService.insert(remindDob);
        return "插入成功";
    }
}
